package com.taxi.driver.module.main.mine.setting;

import android.app.Dialog;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taxi.driver.BuildConfig;
import com.taxi.driver.R;
import com.taxi.driver.common.AppConfig;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.event.UserEvent;
import com.taxi.driver.module.login.LoginActivity;
import com.taxi.driver.module.main.mine.setting.dagger.DaggerSettingComponent;
import com.taxi.driver.module.main.mine.setting.dagger.SettingModule;
import com.taxi.driver.util.BuglyUtils;
import com.taxi.driver.util.Navigate;
import com.taxi.driver.widget.dialog.DialogCreator;
import com.taxi.driver.widget.dialog.OnDefaultListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的-设置
 */
public class SettingActivity extends BaseActivity implements SettingContract.View,
        CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.rl_modify_title)
    RelativeLayout rl_modify_title;
    @BindView(R.id.switch_light)
    SwitchCompat switch_light;
    @BindView(R.id.rl_volume)
    RelativeLayout rl_volume;
    @BindView(R.id.rl_update)
    RelativeLayout rl_update;
    @BindView(R.id.rl_about)
    RelativeLayout rl_about;
    @BindView(R.id.rl_logout)
    RelativeLayout rl_logout;
    @BindView(R.id.tv_debug)
    TextView mTvDebug;

    @Inject
    SettingPresenter mPresenter;
    Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        DaggerSettingComponent.builder()
                .appComponent(Application.getAppComponent())
                .settingModule(new SettingModule(this))
                .build().inject(this);

        EventBus.getDefault().register(this);
        switch_light.setOnCheckedChangeListener(this);
        switch_light.setChecked(mPresenter.getScreenStatue());

        if (AppConfig.isCarpool()) {
            rl_volume.setVisibility(View.GONE);
        }
        showDebugVersion(); //测试版本，显示版本号
    }

    @OnClick({R.id.rl_modify_title, R.id.rl_volume, R.id.rl_update,
            R.id.rl_about, R.id.rl_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_modify_title:
                Navigate.openPwdModify(SettingActivity.this);
                break;
            case R.id.rl_volume:
                Navigate.openVolume(SettingActivity.this);
                break;
            case R.id.rl_update:
                BuglyUtils.checkUpdate();
                break;
            case R.id.rl_about:
                Navigate.openAbout(SettingActivity.this);
                break;
            case R.id.rl_logout:
                showDialog();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventLogout(UserEvent userEvent) {
        if (userEvent.type == UserEvent.LOGOUT) {
            finish();
        }
    }

    public void showDialog() {
        dialog = DialogCreator.defaultDialog(this, getResources().getString(R.string.logout_account), true, new OnDefaultListener() {
            @Override
            public void onCancel() {
                if (dialog != null) {
                    dialog.dismiss();
                    dialog = null;
                }
            }

            @Override
            public void onComfig() {
                mPresenter.reqLogout();

                if (dialog != null) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
        });
        dialog.show();
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            bright();
        } else {
            cancelBright();
        }
        mPresenter.setScreenStatue(isChecked);
    }

    @Override
    public void logoutSuccess() {
        LoginActivity.start(this);
        finish();
    }

    @Override
    public void showDebugVersion() {
        if (!BuildConfig.DEBUG) return;
        mTvDebug.setVisibility(View.VISIBLE);
        mTvDebug.setText(getVerName());
    }


    /**
     * 得到软件显示版本信息
     */
    private String getVerName() {
        String verName = "1.0";
        try {
            String packageName = getPackageName();
            verName = getPackageManager().getPackageInfo(packageName, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "版本号：V" + verName;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
